# -*- coding: UTF-8 -*- #
"""
-------真正的勇士， 敢于直面惨淡的Warning、 敢于正视淋漓的Error-----
@author : 贾鹏博
@file : 17、触屏操作自动化.py
@time : 2022/5/28 13:27
"""

"""
TouchAction 用法
    press     按下
    release   释放
    moveTo    移动
    tap       点击
    wait      等待
    longPress 长按
    cancel    取消
    perform   执行
    TouchAction().press(ele0).moveTo(ele1).release()    按住拖动后释放
    
    同时用两个手指点击同一个元素
    action0 = TouchAction().tap(el)
    action1 = TouchAction().tap(el)
    MultiAction().add(action0).add(action1).perform()
    
手势操作 ： 手机图案解锁
    TouchAction(driver).press(x=244,y=374).wait(100).move_to(x=711,y=374).release().perform()
    

"""

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction


class Test_action():

    def setup(self):
        desired_caps = {}
        desired_caps['platformName'] = "Android"
        desired_caps['platformVersion'] = '6.0'
        desired_caps['deviceName'] = 'emulator-5554'
        desired_caps['appPackage'] = 'com.xueqiu.android'
        desired_caps['appActivity'] = 'com.xueqiu.android.common.MainActivity'
        desired_caps['noReset'] = 'true'
        # desired_caps['dontStopAppOnReset'] = 'true'
        desired_caps['skipDeviceInitialization'] = 'true'
        desired_caps['unicodeKeyBoard'] = 'true'
        desired_caps['resetKeyBoard'] = 'true'
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(10)

    def teardown(self):
        self.driver.quit()

    # 移动操作
    def test_touchaction(self):
        action = TouchAction(self.driver)
        # 获取屏幕尺寸
        width_rect = self.driver.get_window_rect()
        # 屏幕的像素
        # 结果是： {'width': 720, 'height': 1280, 'x': 0, 'y': 0}
        print(width_rect)
        width = width_rect['width']
        height = width_rect['height']
        x1 = int(width / 2)
        y_start = int(height * 4 / 5)
        y_end = int(height * 1 / 5)
        action.press(x=x1, y=y_start).wait(200).move_to(x=x1, y=y_end).release().perform()


class TestTouchAction():

    def setup(self):
        desired_caps = {}
        desired_caps['platformName'] = "Android"
        desired_caps['platformVersion'] = '6.0'
        desired_caps['deviceName'] = 'emulator-5554'
        desired_caps['appPackage'] = 'cn.kmob.screenfingermovelock'
        desired_caps['appActivity'] = 'com.samsung.ui.MainActivity'
        desired_caps['noReset'] = 'true'
        # desired_caps['dontStopAppOnReset'] = 'true'
        desired_caps['skipDeviceInitialization'] = 'true'
        desired_caps['unicodeKeyBoard'] = 'true'
        desired_caps['resetKeyBoard'] = 'true'
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(10)

    def teardown(self):
        self.driver.quit()

    def test_touchaction_unlock(self):
        print('解锁手势密码')
        action = TouchAction(self.driver)
        action.press(x=243, y=395).wait(200).move_to(x=721, y=378).wait(200).move_to(x=1190, y=364).wait(200). \
            move_to(x=1202, y=859).wait(200).move_to(x=195, y=1339).release().perform()
